package com.jiaoxiaobo.consumer;

import com.jiaoxiaobo.RabbitMQUtil;
import com.rabbitmq.client.*;

import java.io.IOException;

public class PushConsumer {

    private static final String QUEUE_NAME = "hello-class-queue";

    public static void main(String[] args) throws Exception {
        Connection connection = RabbitMQUtil.getConnection();
        Channel channel = connection.createChannel();
        channel.queueDeclare(QUEUE_NAME,false,false,false,null);
        DefaultConsumer consumer = new DefaultConsumer(channel) {
            @Override
            public void handleDelivery(String consumerTag,
                                       Envelope envelope,
                                       AMQP.BasicProperties properties,
                                       byte[] body) throws IOException {
                System.out.println("========================");
                String routingKey = envelope.getRoutingKey();
                System.out.println("routingKey >"+routingKey);
                String contentType = properties.getContentType();
                System.out.println("contentType >"+contentType);
                long deliveryTag = envelope.getDeliveryTag();
                System.out.println("deliveryTag >"+deliveryTag);
                System.out.println("content:"+new String(body,"UTF-8"));
                System.out.println("messageId:"+properties.getMessageId());
                properties.getHeaders().forEach((key,value)-> System.out.println("key: "+key +"; value: "+value));
                channel.basicAck(deliveryTag,false);
            }
        };
        channel.basicConsume(QUEUE_NAME,false,consumer);
    }

}
